package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exxceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-23
 */
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;
    //添加小节

    //注入vodclient
    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo)
    {
        eduVideoService.save(eduVideo);
        return R.ok();
    }
    //删除小节
    //TODO 后面需要完善  :删除小节的时候同时把里面的视频删除  远程调用
    @DeleteMapping("deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable String videoId)
    {
        EduVideo byId = eduVideoService.getById(videoId);
        String videoSourceId = byId.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            R r = vodClient.removeAlyVideo(videoSourceId);
            if(r.getCode()==20001){
                System.out.println("熔断器");

                throw new GuliException(20001,"熔断器");

            }
        }

        boolean b = eduVideoService.removeById(videoId);
        if(b)
        {
            return R.ok();
        }
        return R.error();
    }

    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo)
    {

        boolean b = eduVideoService.updateById(eduVideo);
        if(b){
            return R.ok();
        }
        return R.error();
    }

    @GetMapping("{id}")
    public R getVideo(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return R.ok().data("video",eduVideo);
    }

}

